# app/utils/response.py
from flask import jsonify
from typing import Any, Dict, Optional


class Response:
    """统一响应类"""

    @staticmethod
    def success(data: Any = None, message: str = "操作成功", code: int = 200) -> Dict[str, Any]:
        """
        成功响应

        Args:
            data: 响应数据
            message: 响应消息
            code: 状态码

        Returns:
            格式化的响应字典
        """
        return {
            "code": code,
            "data": data if data is not None else {},
            "msg": message
        }

    @staticmethod
    def error(message: str = "操作失败", code: int = 400, data: Any = None) -> Dict[str, Any]:
        """
        错误响应

        Args:
            message: 错误消息
            code: 错误码
            data: 额外数据

        Returns:
            格式化的错误响应字典
        """
        return {
            "code": code,
            "data": data if data is not None else {},
            "msg": message
        }

    @staticmethod
    def unauthorized(message: str = "未授权访问") -> Dict[str, Any]:
        """未授权响应"""
        return Response.error(message=message, code=401)

    @staticmethod
    def forbidden(message: str = "禁止访问") -> Dict[str, Any]:
        """禁止访问响应"""
        return Response.error(message=message, code=403)

    @staticmethod
    def not_found(message: str = "资源不存在") -> Dict[str, Any]:
        """资源不存在响应"""
        return Response.error(message=message, code=404)

    @staticmethod
    def server_error(message: str = "服务器内部错误") -> Dict[str, Any]:
        """服务器错误响应"""
        return Response.error(message=message, code=500)

    @staticmethod
    def validation_error(message: str = "参数验证失败", data: Any = None) -> Dict[str, Any]:
        """参数验证失败响应"""
        return Response.error(message=message, code=422, data=data)

    @staticmethod
    def paginate(data: Any, total: int, page: int, size: int, message: str = "获取成功") -> Dict[str, Any]:
        """
        分页数据响应

        Args:
            data: 分页数据
            total: 总记录数
            page: 当前页码
            size: 每页大小
            message: 响应消息
        """
        return Response.success({
            "list": data,
            "pagination": {
                "total": total,
                "page": page,
                "size": size,
                "pages": (total + size - 1) // size  # 计算总页数
            }
        }, message=message)


class APIResponse:
    """API响应工具类，直接返回Flask jsonify对象"""

    @staticmethod
    def success(data: Any = None, message: str = "操作成功", code: int = 200):
        """成功响应"""
        return jsonify(Response.success(data, message, code))

    @staticmethod
    def error(message: str = "操作失败", code: int = 400, data: Any = None):
        """错误响应"""
        return jsonify(Response.error(message, code, data)), code

    @staticmethod
    def unauthorized(message: str = "未授权访问"):
        """未授权响应"""
        return jsonify(Response.unauthorized(message)), 401

    @staticmethod
    def forbidden(message: str = "禁止访问"):
        """禁止访问响应"""
        return jsonify(Response.forbidden(message)), 403

    @staticmethod
    def not_found(message: str = "资源不存在"):
        """资源不存在响应"""
        return jsonify(Response.not_found(message)), 404

    @staticmethod
    def server_error(message: str = "服务器内部错误"):
        """服务器错误响应"""
        return jsonify(Response.server_error(message)), 500

    @staticmethod
    def validation_error(message: str = "参数验证失败", data: Any = None):
        """参数验证失败响应"""
        return jsonify(Response.validation_error(message, data)), 422

    @staticmethod
    def paginate(data: Any, total: int, page: int, size: int, message: str = "获取成功"):
        """分页数据响应"""
        return jsonify(Response.paginate(data, total, page, size, message))